﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml;

namespace EzDeploy.Core
{
    public class BindingRedirectConfigurationReader : IBindingRedirectConfigurationReader
    {
        // Modified code from http://stackoverflow.com/a/809667
        public List<BindingRedirect> Read(Project project, string value)
        {
            var bindingRedirects = new List<BindingRedirect>();

            string path = project.AssemblyFile + ".config";

            if (!File.Exists(path))
                new List<BindingRedirect>();

            var doc = new XmlDocument();

            try
            {
                doc.Load(path);
            }
            catch (FileNotFoundException)
            {
                new List<BindingRedirect>();
            }

            var manager = new XmlNamespaceManager(doc.NameTable);
            manager.AddNamespace("bindings", "urn:schemas-microsoft-com:asm.v1");

            XmlNode root = doc.DocumentElement;

            var dependentAssemblyNodes = root.SelectNodes("//bindings:dependentAssembly", manager);

            try
            {
                if (dependentAssemblyNodes != null)
                {
                    foreach (XmlNode dependentAssemblyNode in dependentAssemblyNodes)
                    {
                        string assemblyName = null, publicKeyToken = null, culture = null, oldVersion = null, newVersion = null;
                        bool publisherPolicy = true;

                        foreach (XmlNode node in dependentAssemblyNode.ChildNodes)
                        {
                            if (node.NodeType != XmlNodeType.Element)
                                continue;

                            switch (node.Name)
                            {
                                case "assemblyIdentity":
                                    assemblyName = node.Attributes["name"].Value;
                                    publicKeyToken = node.Attributes["publicKeyToken"].Value;
                                    culture = node.Attributes["culture"].Value;
                                    break;
                                case "bindingRedirect":
                                    var oldVersionAttr = node.Attributes["oldVersion"];
                                    oldVersion = oldVersionAttr != null ? oldVersionAttr.Value : string.Empty;
                                    var newVersionAttr = node.Attributes["newVersion"];
                                    newVersion = newVersionAttr != null ? newVersionAttr.Value : string.Empty;
                                    break;
                                case "publisherPolicy":
                                    publisherPolicy = node.Attributes["apply"].Value == "yes";
                                    break;
                            }
                        }

                        if (!string.IsNullOrEmpty(assemblyName))
                            bindingRedirects.Add(new BindingRedirect(assemblyName, publicKeyToken, culture, oldVersion, newVersion, publisherPolicy));
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bindingRedirects;
        }
    }
}